<?php

/***  DOCUMENTATION LAYER

Last Update: Mar 2007
	
DESCRIPTION
	script intended to notify user of changes in a wikka wiki via cron jobs
	
INSTALLATION

	I've tried to minimize changes needed to configure.  Unfortunately, because of
	the wikka .htaccess settings, you can't put it in the wikka directory.  I usually
	upload to a root directory.
	
	To test, set $_SET['call_directly'] = 1 and call page in browser.
	
	Coordinate cron tab with $_SET['update_period'] -- i.e., if it is set to 6 (hours),
	run your cron every 6 hours.  With my cPanel accounts, I use the following command:
	
		lynx -dump 'http://yourdomain.com/wikka_changemail/cron.changemail.php'
		
	Designed to be used with GPL SimplePie and PHPMailer packages -- either reset path
	settings for these packages below or make sure they're in this directory
	
NOTES	
	make sure the cache dir is writeable
	
LICENSE
	GPL : see http://www.opensource.org/licenses/gpl-license.php
	
***/

// * Pre-Script
$_tx0 = microtime();
define('_DS', DIRECTORY_SEPARATOR);

// * Path Settings
$_DIR['super'] = dirname($_SERVER['DOCUMENT_ROOT']) . _DS;
$_DIR['this_root'] = dirname(__FILE__) . _DS;
$_DIR['wikka_root'] = $_SERVER['DOCUMENT_ROOT'] . _DS . 'wikka' . _DS;
$_DIR['simplepie'] = $_DIR['this_root'] . 'SimplePie' . _DS;
$_DIR['phpmailer'] = $_DIR['this_root'] . 'phpmailer' . _DS;
$_PATH['simplepie_cache'] = $_DIR['this_root'] . 'cache' . _DS;

// * Script Settings
$_SET['update_period'] = 6;	// hours 
$_SET['wikka_xml_feed'] = 'http://DOMAIN.com/wikka' . '/RecentChanges/recentchanges.xml';
$_SET['email_to'] = 'YOUR_EMAIL@gmail.com';
$_SET['email_to_name'] = 'Wikka Admin';
$_SET['email_from_name'] = 'Wikka Update Notifier';
$_SET['email_from'] = $_SERVER['SERVER_ADMIN'];
$_SET['email_subject'] = 'Wikka Changes Update : ' . date('r');
$_SET['call_directly'] = 0;		// for testing, set to 0 to run as cron exclusively

// Variable Declarations
$_TRIGGER['mail'] = 0;
$_MAIL['body'] = '';

// * Header
echo "<h4 style='margin:0 0 16px;'>Wikka Recent Changes</h4>";	// put script name here


// *** PROCEDURAL
if ( 1 ) 
{
	// access control
	if ( !$_SET['call_directly'] && ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']) )  die('run from cron only');

	// load SimplePie
	require_once($_DIR['simplepie'] . 'simplepie.inc');
	
	// create SimplePie handler	
	$FeedHandler = new SimplePie();
	$FeedHandler->cache_location($_PATH['simplepie_cache']);
	$FeedHandler->output_encoding('ISO-8859-1');
	
	// fetch url
	$FeedHandler->feed_url($_SET['wikka_xml_feed']);
	$FeedHandler->init();
	$FeedHandler->handle_content_type();
	
	// * DEBUG
	if ( 0 ) print_r($FeedHandler);
	
	// get items
	if ( $FeedHandler->data )
  {
  	if ( 0 ) print_r($FeedHandler->data);		// debug
		$_DATA['title'] = $FeedHandler->data['info']['title'];
		$_DATA['ITEM'] = $FeedHandler->get_items();
		$_URL['home'] = $FeedHandler->data['info']['link']['alternate'][0];
  }
	else
	{
		trigger_error("SimplePie failed -- unable to fetch feed {$FeedHandler->url}", E_USER_WARNING);
 		die;
	}
	
	foreach ( $_DATA['ITEM'] as $_SimpleItem )
	{
		$_ARRAY = $_SimpleItem->data;
		$_title = $_ARRAY['title'];
		$_descrip = $_ARRAY['description'];
		$_link = $_URL['home'] . $_title;
		$_update_link = $_ARRAY['link']['alternate'][0];
		
		// time
		$_age = time() - $_ARRAY['pubdate'];
		$_days = floor($_age / (24 * 60 * 60));
		$_hours = floor(($_age / (60 * 60)) % 24);
		$_mins = floor(($_age / 60) % 60);
		$_secs = $_age % 60;
		$_time = "$_days days $_hours hours $_mins mins $_secs secs";
		
		// collect data
		$_DATA['CHANGE'][] = "$_link\n$_title ($_descrip)\nupdated $_time ago\n\n";
		
		// recent
		$max_age = $_SET['update_period'] * 60 * 60;
		if ( $_age < $max_age )
		{
			$_DATA['RECENT'][] = "$_link\n$_title ($_descrip)\nupdated $_time ago\n\n";
		}		
	}
	
	// count recent
	$_DATA['change_count'] = count($_DATA['RECENT']);
	
	// set mail trigger
	if ( $_DATA['change_count'] > 0 ) $_TRIGGER['mail'] = 1;
	else echo "<h5 style='color:green;'>no new items -> don't send email</h5>"; 
	
	// * Mail?
	if ( $_TRIGGER['mail'] )
	{
		// call PHP Mailer library
		require($_DIR['phpmailer'] . 'class.phpmailer.php');
	
		// build text body
		$_MAIL['body'] = "{$_DATA['change_count']} changes to {$_URL['home']} in last {$_SET['update_period']} hours \r\n\r\n";
		foreach ( $_DATA['RECENT'] as $_text ) $_MAIL['body'] .= "$_text \r\n";
		
		// PHP Mailer object
		$Mailer = new PHPMailer();
		$Mailer->FromName = $_SET['email_from_name'];
		$Mailer->From = $_SET['email_from'];
		$Mailer->Body = $_MAIL['body'];
		$Mailer->Subject = $_SET['email_subject'];
    $Mailer->AddAddress($_SET['email_to'], $_SET['email_to_name']);
		
		// Send
		if ( !$Mailer->Send() )
		{
			trigger_error("failed to send update to {$_SET['email_to']}: \n\n {$_MAIL['body']}", E_USER_WARNING);
		}
		else
		{
			echo "<h5 style='color:green;'>mail successfully sent</h5>";
		}
	}
}



// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
$footer = <<<HTML
<h4 style="margin:0; position:absolute; top:6px; right:6px; font-family:monospace; color:#36f; padding:4px; border:1px solid #36f; font-weight:normal; background:#f3f6ff;">
script completed in $stx s
</h4>
HTML;
echo $footer;

?>
